package guyubao.com.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import guyubao.com.entity.EmailModel;
import guyubao.com.entity.RestBean;
import guyubao.com.entity.vo.request.MessageChannelVoRequest;
import guyubao.com.entity.vo.response.MessageChannelVo;
import guyubao.com.service.MessageChannelService;
import guyubao.com.service.RedisService;
import guyubao.com.utils.SendMailMessageUtils;
import guyubao.com.utils.SendPushPlusMessageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@Slf4j
@RestController
@RequestMapping("/api/messageChannel")
@Tag(name = "消息渠道相关", description = "消息渠道的增删改查")
public class MessageChannelController extends BaseController {
    @Resource
    MessageChannelService messageChannelService;
    @Resource
    RedisService redisService;
    @Resource
    SendPushPlusMessageUtils sendPushPlusMessageUtils;
    @Resource
    SendMailMessageUtils sendMailMessageUtils;
    // 获得消息渠道信息
    @Operation(summary ="根据uId查询消息渠道")
    @GetMapping("/uId")
    public RestBean<MessageChannelVo> getMessageChannel(@RequestParam @Length(min=1,max = 20) String uid,
                                                        @RequestAttribute String id){
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification!=null) {
            return RestBean.forbidden(verification);
        }
        return this.innerGetMessageChannel(uid);
    }
    @Operation(summary ="内部根据uId查询消息渠道")
    public RestBean<MessageChannelVo> innerGetMessageChannel(String uid){
        return RestBean.success(messageChannelService.getMessageChannel(uid));
    }
    // 修改消息渠道信息
    @Operation(summary = "根据uId修改消息渠道")
    @PutMapping("/update")
    public RestBean<Void> updateMessageChannel(@RequestBody @Valid MessageChannelVoRequest vo,
                                               @RequestAttribute String email,
                                               @RequestAttribute String id){
        // 验证该账号是否属于用户
        String verification = verification(vo.getUid(), id);
        if (verification!=null) {
            return RestBean.forbidden(verification);
        }
        // 获得邮箱
        if (email==null) {
            return RestBean.failure(500,"出错了没有获得您的邮箱，请联系管理人员！");
        }
        // 获得redis中验证码并判断
        String verificationCode = redisService.verificationCode(vo.getCode(), "message", email);
        if (verificationCode!=null) {
            return RestBean.failure(400,verificationCode);
        }
        // 执行业务
        return this.innerUpdateMessageChannel(vo);
    }
    // 修改消息渠道信息
    public RestBean<Void> innerUpdateMessageChannel(@RequestBody @Valid MessageChannelVoRequest vo){
        try {
            // 如果是清除语句没有token
            JSONObject jsonObject = JSON.parseObject(vo.getJson());
            // 如果是pushplus渠道
            String token = jsonObject.get("token").toString();
            String emailAddress = jsonObject.get("receiverEmailAddress").toString();
            if (token != null) {
                sendPushPlusMessageUtils.send(token, "测试推送", "欢迎您开启消息推送~~");
            }
            // 邮件渠道
            else if (emailAddress != null){
                sendMailMessageUtils.sendEmailAsText(new EmailModel(emailAddress, "测试推送", "欢迎您开启消息推送~~"));
            }
        } catch (Exception e) {
            log.error("测试推送渠道出现问题，原因{}",e.getMessage());
        }
        return this.messageHandle(vo,messageChannelService::updateMessageChannel);
    }
}
